package ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ssm.pojo.Pager;
import ssm.pojo.User;
import ssm.service.IUserService;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Author: silence
 * Date: 17-5-26
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private IUserService userService;

    @RequestMapping("/users/{id}")
    public String toIndex(@PathVariable Integer id, Model model) {
        User user = userService.getUserById(id);
        model.addAttribute("user", user);
        return "update";
    }

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public String listAllUser(Model model) {
        /*获取所有队伍的信息*/
        List<User> users = (List<User>) userService.findAllByQuery();
        model.addAttribute("userList", users);
        return "showUser";
    }

    @RequestMapping(value = "/listpage", method = RequestMethod.GET)
    public String listpage(String Pageindex,Model model) {
        Pager page = new Pager();
        page.setPageSize(5);

        int count = userService.count() / page.getPageSize();
        if (userService.count() % page.getPageSize() > 0) {
            count = count + 1;

        }
        int[] s=new int[count];
        for(int i=0;i<s.length;i++){
            s[i]=i;
        }
        page.setAllPage(s);
        page.setAllPages(s.length);
        List<User> users;
        if (Pageindex == null) {
            page.setPageIndex(0);
            users = (List<User>) userService.selectPageList(page.getPageIndex(), page.getPageSize());
        } else {
            page.setPageIndex(Integer.valueOf(Pageindex));
            users = (List<User>) userService.selectPageList(page.getPageIndex(), page.getPageSize());
        }
        page.setDatas(users);
        model.addAttribute("page", page);
        return "showUserPage";
    }

    /***
     * 添加用户信息
     *
     * @param model
     * @return
     */
    //链接到add页面时是GET请求，会访问这段代码，注册表单时先判断用户
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        //开启modelDriven
        model.addAttribute(new User());
        return "student_add";
    }

    //进入用户注册界面判断用户名是否已经存在，使用user.getUserName方法
    //在具体添加用户时，是post请求，就访问以下代码
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(User user, Model model) throws UnsupportedEncodingException {
        User u = userService.getUserById(user.getUserId());
        if (!(u == null)) {
            String test = "用户存";
            model.addAttribute("error", new String(test.getBytes("ISO-8859-1"), "UTF-8"));
            return "student_add";
        }
        String inser = userService.insert(user);
        if (inser.equals("添加成功")) {
            return "redirect:/user/users";
        }

        model.addAttribute("error", inser);

        return "student_add";
    }

    /***
     * 更新一个用户信息
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@Validated User user) {
        User user1 = userService.getUserById(user.getUserId());
        user1.setUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        user1.setDescription(user.getDescription());
        userService.updateByPrimaryKey(user1);
        return "updateSuccess";
    }

    /**
     * 删除一个用户信息
     */
    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable Integer id, Model model) {
        userService.deleteByPrimaryKey(id);
        System.out.println("ooooooooooooooooooooo" + id);
        model.addAttribute("usern", id);
        return "delete";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    /**
     * 用户登录方法
     *
     * @param username
     * @param password
     * @param model
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String username, String password, Model model) {
        if ("yuweimei".equals(username) && "123456".equals(password))
            return "redirect:/user/users";
        return "login";
    }

}

